package com.linsen.erp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsen.core.common.util.ConvertUtil;
import com.linsen.erp.admin.entity.EmployeeReportingEntity;
import com.linsen.erp.admin.mapper.EmployeeReportingMapper;
import com.linsen.erp.admin.provider.dto.EmployeeReportingDTO;
import com.linsen.erp.admin.provider.vo.EmployeeReportingVO;
import com.linsen.erp.admin.service.IEmployeeReportingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 员工-汇报关系 服务实现类
 * </p>
 *
 * @author zhaoyong
 * @since 2024-10-08
 */
@Service
@RequiredArgsConstructor
public class EmployeeReportingServiceImpl extends ServiceImpl<EmployeeReportingMapper, EmployeeReportingEntity> implements IEmployeeReportingService {

    @Override
    public EmployeeReportingVO getByEmployeeId(String employeeId) {
        return ConvertUtil.convert(this.getOne(
                new LambdaQueryWrapper<EmployeeReportingEntity>()
                        .eq(EmployeeReportingEntity::getEmployeeId,employeeId)
        ), EmployeeReportingVO.class);
    }

    @Transactional
    @Override
    public void set(String employeeId, EmployeeReportingDTO employeeReportingDTO) {
        //清除历史数据
        this.remove(
                new LambdaQueryWrapper<EmployeeReportingEntity>()
                        .eq(EmployeeReportingEntity::getEmployeeId,employeeId)
        );

        EmployeeReportingEntity employeeReportingEntity = ConvertUtil.convert(employeeReportingDTO, EmployeeReportingEntity.class);
        employeeReportingEntity.setEmployeeId(employeeId);
        this.save(employeeReportingEntity);
    }
}
